package ${package.Controller};

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.cocam.erp.base.bean.Result;
import com.cocam.erp.base.bean.ResultCode;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cocam.erp.utils.ExcelUtils;
import com.cocam.erp.utils.UserUtils;
import java.util.Date;
import com.cocam.erp.utils.DateUtil;
import com.cocam.erp.validator.*;
import java.util.List;
<#if restControllerStyle>
    import org.springframework.web.bind.annotation.RestController;
<#else>
    import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
    import ${superControllerClassPackage};
</#if>

/**
* <p>
    * ${table.comment!} 控制器层
    * </p>
*
* @classname: ${table.controllerName}
* @author: ${author}
* @since: ${date}
* @description: ${table.comment!} 控制器层
* @towork: none
* @version: v1.0.0
*/
@Slf4j
<#if restControllerStyle>
    @Api(tags = {"${table.comment!}"})
    @RestController
<#else>
    @Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
        public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
        public class ${table.controllerName} {
    </#if>
    /**
    * ${table.comment!} 服务层实现实例化对象
    */
    @Autowired
    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};

    /**
    * <h2>列表查询</h2>
    *
    * @param ${entity?uncap_first} ${table.comment!}实体类
    * @return
    */
    @ApiOperation(value = "列表页查询")
    @RequestMapping(value = "/selectList")
    public Result selectList(@RequestBody @Validated({PageGroup.class}) ${entity} ${entity?uncap_first}, BindingResult bindingResult){
    //参数校验
    if (bindingResult.hasErrors()) {
    return new Result(ResultCode.ERR_PARAM_FORMAT, bindingResult.getFieldError().getDefaultMessage());
    }
    Result result = new Result();
    try {
    PageHelper.startPage(${entity?uncap_first}.getPageNum(), ${entity?uncap_first}.getPageSize());
    List<${entity}>  list = ${(table.serviceName?substring(1))?uncap_first}.selectList(${entity?uncap_first});
    result.setData(new PageInfo<>(list));
    }
    catch (Exception e) {
    log.error("${table.comment!}-查询列表页失败:{}", e);
    result = result.errorSystem("查询列表页失败");
    }
    return result;
    }

    /**
    * <h2>查询详情页</h2>
    *
    * @param id 主键id
    * @return
    */
    @ApiOperation(value = "根据id查询数据")
    @RequestMapping(value = "/getById")
    public Result getById(@RequestParam("id") Long id){
    Result result = new Result();
    try {
    ${entity} ${entity?uncap_first} = ${(table.serviceName?substring(1))?uncap_first}.getById(id);
    result.setData(${entity?uncap_first});
    }
    catch (Exception e) {
    log.error("${table.comment!}-查询详情页失败:{}", e);
    result = result.errorSystem("查询详情页失败");
    }
    return result;
    }
    /**
    * <h2>新增</h2>
    *
    * @param ${entity?uncap_first} 新增功能 视图对象
    * @param bindingResult         校验结果返回对象
    * @return
    */
    @ApiOperation(value = "新增数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody @Validated(InsertGroup.class) ${entity} ${entity?uncap_first}, BindingResult bindingResult){
    //参数校验
    if (bindingResult.hasErrors()) {
    return new Result(ResultCode.ERR_PARAM_FORMAT, bindingResult.getFieldError().getDefaultMessage());
    }

    //${entity?uncap_first}.setUser_key(UserUtils.getCurrentUserInfo().getUser_key());

    //逻辑执行
    Result result = new Result();
    try {
    ${(table.serviceName?substring(1))?uncap_first}.add(${entity?uncap_first});
    }
    catch (Exception e) {
    log.error("${table.comment!}-新增失败:{}", e);
    result = result.errorSystem("新增失败");
    }
    return result;
    }

    /**
    * <h2>更新数据</h2>
    *
    * @param ${entity?uncap_first} 更新功能视图对象
    * @param bindingResult  校验结果返回对象
    * @return
    */
    @ApiOperation(value = "更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody @Validated(UpdateGroup.class) ${entity} ${entity?uncap_first}, BindingResult bindingResult){
    //参数校验
    if (bindingResult.hasErrors()) {
    return new Result(ResultCode.ERR_PARAM_FORMAT, bindingResult.getFieldError().getDefaultMessage());
    }

    //${entity?uncap_first}.setUser_key(UserUtils.getCurrentUserInfo().getUser_key());

    //逻辑执行
    Result result = new Result();
    try {
    ${(table.serviceName?substring(1))?uncap_first}.update(${entity?uncap_first});
    }
    catch (Exception e) {
    log.error("${table.comment!}-更新失败:{}", e);
    result = result.errorSystem("更新失败");
    }
    return result;
    }

    /**
    * <h2>删除</h2>
    *
    * @param id 主键id
    * @return
    */
    @ApiOperation(value = "删除数据")
    @RequestMapping(value = "/del")
    public Result delete(@RequestParam("id") Long id){
    Result result = new Result();
    try {
    ${(table.serviceName?substring(1))?uncap_first}.delete(id);
    }
    catch (Exception e) {
    log.error("${table.comment!}-删除数据失败:{}", e);
    result = result.errorSystem("删除失败");
    }
    return result;
    }
    }
</#if>